package com.lvmama.service.rhino.topic.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.common.enums.ErrorCodeEnum;
import com.lvmama.common.exception.BizException;
import com.lvmama.dao.rhino.topic.StatisticCodeDao;
import com.lvmama.dao.rhino.topic.TaskSequenceDao;
import com.lvmama.pojo.entity.rhino.topic.StatisticCode;
import com.lvmama.pojo.dto.rhino.topic.StatisticCodesConditionVO;
import com.lvmama.pojo.entity.rhino.topic.TaskSequence;
import com.lvmama.service.rhino.topic.StatisticCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StatisticCodeServiceImpl implements StatisticCodeService, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     *
     */
    @Autowired
    private StatisticCodeDao statisticCodeDao;

    /**
     *
     */
    @Autowired
    private TaskSequenceDao taskSequenceDao;


    /**
     * 分页查询统计code码信息
     *
     * @param sc
     * @return
     */
    @Override
    public PageInfo<StatisticCode> getAllList(StatisticCodesConditionVO sc) {
        PageHelper.startPage(sc.getCurrentPage(), sc.getPageSize());
        List<StatisticCode> statisticCodes = statisticCodeDao.getAllList(sc);
        PageInfo<StatisticCode> page = new PageInfo<StatisticCode>(statisticCodes);
        return page;
    }

    @Override
    public Integer insertStatisticCodeObj(StatisticCode statisticCode) {
        return statisticCodeDao.insertStatisticCodeObj(statisticCode);
    }

    @Override
    public Integer insertOtherStatisticCodeObj(StatisticCode statisticCode) {
        return statisticCodeDao.insertOtherStatisticCodeObj(statisticCode);
    }

    @Override
    public Integer getCountForCode(String code) {
        return statisticCodeDao.getCountForCode(code);
    }

    /**
     * 获取统计code码数据
     *
     * @return
     */
    @Override
    public List<StatisticCode> getStatisticCodeList() {
        return statisticCodeDao.getAllList(null);
    }

    /**
     * 根据条件查询code对象
     *
     * @param statisticCode
     * @return
     */
    @Override
    public StatisticCode selectStatisticCode(StatisticCode statisticCode) {
        return statisticCodeDao.selectStatisticCode(statisticCode);
    }

    /**
     * 查询所有包含该前缀的code码信息
     *
     * @return
     */
    @Override
    public List<String> getLikeCodes(String levelCode) {
        return statisticCodeDao.getLikeCodes(levelCode);
    }

    @Override
    public List<String> getZhtiLikeCode(String levelCode) {
        return statisticCodeDao.getZhtiLikeCode(levelCode);
    }

    @Override
    public List<String> getPitLikeCode() {
        return statisticCodeDao.getPitLikeCode();
    }

    /**
     * 查询name是否存在
     *
     * @param name
     * @return
     */
    @Override
    public int getCountForName(String name) {
        return statisticCodeDao.getCountForName(name);
    }


    /**
     * 操作code码信息
     *
     * @param statisticCode
     * @return
     */
    @Override
    @Transactional(value = "rhinoTransactionalManager",rollbackFor = Exception.class)
    public int operationCode(StatisticCode statisticCode)throws BizException {
        try {
            if (statisticCode.getParentId() == 434 || statisticCode.getParentId() == 15) {
                if (statisticCode.getId() != null) {
                    return statisticCodeDao.updateOtherCode(statisticCode);
                } else {
                    TaskSequence taskSequence = new TaskSequence();
                    taskSequenceDao.insertTaskSeqence(taskSequence);
                    statisticCode.setId(taskSequence.getId());
                    return statisticCodeDao.insertOtherStatisticCodeObj(statisticCode);
                }
            } else {
                if (statisticCode.getId() != null) {
                    return statisticCodeDao.updateCode(statisticCode);
                } else {
                    TaskSequence taskSequence = new TaskSequence();
                    taskSequenceDao.insertTaskSeqence(taskSequence);
                    statisticCode.setId(taskSequence.getId());
                    return statisticCodeDao.insertStatisticCodeObj(statisticCode);
                }
            }
        }catch (Exception e){
            log.error("update dataBase error");
            throw new BizException(ErrorCodeEnum.DATA_BASE_ACCESS_ERROR.getCode(),ErrorCodeEnum.DATA_BASE_ACCESS_ERROR.getMsg(),e);
        }

    }

    /**
     * 根据id查询子节点
     *
     * @param id
     * @return
     */
    @Override
    public List<StatisticCode> queryChildCodeListByParentId(Integer id) {
        return statisticCodeDao.queryChildCodeListByParentId(id);
    }

    /**
     * 根据父级code码，查询子级对象
     *
     * @param levelCode
     * @return
     */
    @Override
    public List<StatisticCode> getLikeCodeObj(String levelCode) {
        return statisticCodeDao.getLikeCodeObj(levelCode);
    }


    /**
     * 更新code码信息
     *
     * @param statisticCode
     * @return
     */
    @Override
    public int updateCode(StatisticCode statisticCode) {
        return statisticCodeDao.updateCode(statisticCode);
    }

    @Override
    public int updateOtherCode(StatisticCode statisticCode) {
        return statisticCodeDao.updateOtherCode(statisticCode);
    }

    /**
     * 查询所有前缀为codeName的code码值
     *
     * @param codeName
     * @return
     */
    @Override
    public List<String> getCodeByLikeName(String codeName) {
        return statisticCodeDao.getCodeByLikeName(codeName);
    }


    /**
     * 根据父级code码，查询子级对象(不包含父级对象)
     *
     * @param levelCode
     * @return
     */
    @Override
    public List<StatisticCode> getLikeCodeObjNotParent(String levelCode) {
        return statisticCodeDao.getLikeCodeObjNotParent(levelCode);
    }

    /**
     * 查询所有前缀为codeName的渠道code码值
     *
     * @param codeName
     * @return
     */
    @Override
    public List<String> getDeviceCodeByLikeName(String codeName) {
        return statisticCodeDao.getDeviceCodeByLikeName(codeName);
    }

    /**
     * 查询所有前缀为pc,wap的渠道code码值
     *
     * @param codeName
     * @return
     */
    @Override
    public List<String> getDeviceCodeByLikeNamePcAndWap(String codeName) {
        return statisticCodeDao.getDeviceCodeByLikeNamePcAndWap(codeName);
    }


    /**
     * 根据code码查询父级和子级的code码信息
     * @param levelCode
     * @return
     */
    @Override
    public List<StatisticCode> getParentAndChildrenLikeCodeObj(String levelCode) {
        return statisticCodeDao.getParentAndChildrenLikeCodeObj(levelCode);
    }

    /**
     * 查询app的设备的code码信息
     *
     * @return
     */
    @Override
    public List<String> getDeviceCodeApp() {
        return statisticCodeDao.getDeviceCodeApp();
    }

    @Override
    public List<String> getDeviceCodeNoPC() {
        return statisticCodeDao.getDeviceCodeNoPC();
    }

// /////////////////////////////////////////统计二期/////////////////////////////

    /**
     * 根据父级渠道code码，查询所有子级code
     * @param code
     * @return
     */
    @Override
    public List<String> getChannelCode(String code) {
        return statisticCodeDao.getChannelCode(code);
    }


    /**
     * @param @param  code
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getNameByCode
     * @Description: 根据code获取name
     */
    @Override
    public StatisticCode getNameByCode(String code) {
        return statisticCodeDao.getNameByCode(code);
    }

    @Override
    public List<StatisticCode> getZhtiPitByParentId(List<StatisticCode> statisticCodes, Integer parentId) {
        //根据父id查询出List
        List<StatisticCode> statisticCodeList = statisticCodeDao.queryChildCodeListByParentId(parentId);
        if (null != statisticCodeList && !statisticCodeList.isEmpty()) {
            for (StatisticCode statisticCode : statisticCodeList) {
                statisticCodes.add(statisticCode);
                getZhtiPitByParentId(statisticCodes, statisticCode.getId());
            }
        }
        return statisticCodes;
    }


    @Override
    public List<String> getPageAllSonName() {
        List<String> result = new ArrayList<>();
        StatisticCodesConditionVO sc = new StatisticCodesConditionVO();
        sc.setParentName("页面");
        List<StatisticCode> statisticCodes = statisticCodeDao.getAllList(sc);
        statisticCodes = getZhtiPitByParentId(statisticCodes, statisticCodes.get(0).getParentId());
        for (StatisticCode statisticCode : statisticCodes) {
            if ("0".equals(statisticCode.getIsAdd())) {
                result.add(statisticCode.getName());
            }
        }
        return result;
    }
}
